<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>薪资管理系统 - 登录</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <div class="logo">
                    <span class="money-icon">💰</span>
                </div>
                <h1>薪资管理系统</h1>
                <p>请选择您的身份并登录</p>
            </div>
            
            <div class="role-selector">
                <button class="role-btn active" data-role="admin">管理员</button>
                <button class="role-btn" data-role="employee">员工</button>
            </div>
            
            <form id="loginForm" class="login-form">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required>
                </div>
                
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required>
                </div>
                
                <button type="submit" class="login-btn">登录</button>
            </form>
        </div>
    </div>

    <script>
        // 角色选择
        const roleBtns = document.querySelectorAll('.role-btn');
        let currentRole = 'admin';

        roleBtns.forEach(btn => {
            btn.addEventListener('click', () => {
                roleBtns.forEach(b => b.classList.remove('active'));
                btn.classList.add('active');
                currentRole = btn.dataset.role;
            });
        });

        // 登录表单处理
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password,
                        role: currentRole
                    })
                });
                
                const data = await response.json();
                
                if (data.success) {
                    // 保存token和角色信息
                    localStorage.setItem('token', data.token);
                    localStorage.setItem('role', data.role);
                    localStorage.setItem('username', username);
                    
                    // 根据角色跳转到不同的仪表板
                    if (data.role === 'admin') {
                        window.location.href = 'admin_dashboard.html';
                    } else {
                        window.location.href = 'employee_dashboard.html';
                    }
                } else {
                    alert('登录失败：' + data.message);
                }
            } catch (error) {
                console.error('登录错误：', error);
                alert('登录失败，请检查网络连接');
            }
        });
    </script>
</body>
</html> 